﻿/*-----------------------------------------------
// File: FirstRoundWaitState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class FirstRoundWaitState :RoundWaitState 
    {
        public BattleViewFirstCtrl firstViewCtrl {
            get { return battleRoundCtrl.viewCtrl as BattleViewFirstCtrl; }
        }
        public FirstRoundWaitState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl) { }

        public override void Enter()
        {
            base.Enter();
            firstViewCtrl.isGuideShowing = false;
            currentPhase = RoundWaitPhase.Normal;
        }

        public override void Leave()
        {
            firstViewCtrl.isGuideShowing = false;
            BattleManager.Instance.battlePanel.guidePanel.Hide();
        }

        public override void Update()
        {
            if (currentPhase == RoundWaitPhase.None)
            {
                currentPhase = RoundWaitPhase.Normal;
            }
            else if (currentPhase == RoundWaitPhase.Normal)
            {
                if (firstViewCtrl.curRunningCamp == BattleCamp.Attacker)
                {
                    if ((firstViewCtrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.Begin || firstViewCtrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.NormalSkillNeedFirstCast) && !firstViewCtrl.isGuideShowing)
                    {
                        firstViewCtrl.curGuideStep = BattleViewFirstCtrl.FirstBattleGuideStep.NormalSkillNeedFirstCast;
                        firstViewCtrl.SelectNormalSkillCastFocus();
                        if (!string.IsNullOrEmpty(firstViewCtrl.normalSkillCastFocus))
                        {
                            BattleManager.Instance.battlePanel.guidePanel.Show(BattleManager.Instance.battlePanel.GetBattleHeroItem(firstViewCtrl.normalSkillCastFocus),
                                firstViewCtrl.curGuideStep);
                            firstViewCtrl.isGuideShowing = true;
                        }
                    }
                    else if (firstViewCtrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.NormalSkillNeedSecondCast && !firstViewCtrl.isGuideShowing)
                    {
                    }
                    else if (firstViewCtrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.NormalSkillNeedAllCast && !firstViewCtrl.isGuideShowing)
                    {
                        BattleManager.Instance.battlePanel.guidePanel.Show(BattleManager.Instance.battlePanel.autoButton.gameObject, firstViewCtrl.curGuideStep);
                        firstViewCtrl.isGuideShowing = true;
                    }
                    else if (firstViewCtrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.UltimateSkillNeedFirstCast && !firstViewCtrl.isGuideShowing)
                    {
                        if (IsUltimateCanCasted())
                        {
                            firstViewCtrl.SelectUltimateSkillCastFocus(GetUltimateCanCastActorUID());
                            BattleManager.Instance.battlePanel.guidePanel.Show(BattleManager.Instance.battlePanel.GetBattleHeroItem(firstViewCtrl.ultimateSkillCastFocus), BattleViewFirstCtrl.FirstBattleGuideStep.UltimateSkillNeedFirstCast);
                            firstViewCtrl.isGuideShowing = true;
                        }
                        else {
                            firstViewCtrl.curGuideStep = BattleViewFirstCtrl.FirstBattleGuideStep.Over;
                            BattleManager.Instance.battlePanel.guidePanel.Hide();
                        }
                    }
                }

                if (BattleViewCtrl.GetInstance().isCanBattleOver)
                {
                    BattleManager.Instance.FightOver();
                    return;
                }
                // 引导战斗不会进入自动战斗 
                BattleManager.Instance.battlePanel.HideAutoCountDownLabel();
                // 检测普通技能
                if (BattleViewCtrl.GetInstance().IsHaveBehaviourNeedDo())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.AttackingState);
                    return;
                }
                // 检测大招
                if (BattleViewCtrl.GetInstance().IsHaveUltimateBehaviour())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.UltimateBeforeState);
                    return;
                }
                if (IsAllAttacked())
                {
                    if (IsUltimateCanCasted())
                    {
                        ultimateDeltaTime = 0f;
                        currentPhase = RoundWaitPhase.WaitForUltimate;
                        return;
                    }
                    else {
                        battleRoundCtrl.RoundLeave();
                        return;
                    }
                }


            }
            else if (currentPhase == RoundWaitPhase.WaitForUltimate) {
                ultimateDeltaTime += Time.deltaTime;
                if (BattleViewCtrl.GetInstance().curRunningCamp == BattleCamp.Defender)
                {

                }
                else {
                    var cooldownnum = Mathf.Clamp(UltimateWaitTime - ultimateDeltaTime, 0f, UltimateWaitTime);
                    BattleManager.Instance.battlePanel.ShowCooldown(cooldownnum);
                }

                if (BattleViewCtrl.GetInstance().isCanBattleOver) {
                    BattleManager.Instance.FightOver();
                    return;
                }

                if (ultimateDeltaTime >= UltimateWaitTime)
                {
                    battleRoundCtrl.RoundLeave();
                    return;
                }

                if (BattleViewCtrl.GetInstance().IsHaveUltimateBehaviour())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.UltimateBeforeState);
                    return;
                }
            }
        }

        protected override bool IsUltimateCanCasted()
        {
            return base.IsUltimateCanCasted() && firstViewCtrl.isFirstUltimateCanCast;
        }


        protected string GetUltimateCanCastActorUID()
        {
            List<BattleCharacter> actors = new List<BattleCharacter>();

            foreach (var uid in BattleViewCtrl.GetInstance().GetActorUids(battleRoundCtrl.currentCamp))
            {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                if (bc.IsUltimateSkillCanCast())
                {
                    actors.Add(bc);
                }
            }
            actors.Sort(delegate (BattleCharacter a, BattleCharacter b) {
                return a.FPId.CompareTo(b.FPId);
            });
            if (actors.Count > 0) {
                return actors[0].uniqueId;
            }
            return "";
        }


    }

}